﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace DAO
{
    public abstract class AbstractDAO<T>
    {
        //lay cau truy van lay danh sach day du
        protected abstract string GetQueryLayDanhSach();

        //lay cau truy van insert
        protected abstract string GetInsertQuery();

        //lay cau truy van update
        protected abstract string GetUpdateQuery();

        //lay cau truy van delete
        protected abstract string GetDeleteQuery();

        protected abstract string GetQueryGetByID(string id);

        //get object from query reults
        protected abstract T GetObjectFromDataRow(DataRow dr);

        //lay danh sach tham so tu item
        protected abstract IList<OleDbParameter> GetListParameter(T item);

        //lấy danh sách đầy đủ
        public List<T> LayDanhSach2()
        {
            List<T> list = null;
            try
            {
                list = new List<T>();
                String query = this.GetQueryLayDanhSach();
                DataTable dt = DataProvider.ExecuteQuery(query);
                foreach (DataRow dr in dt.Rows)
                {
                    T item = GetObjectFromDataRow(dr);
                    list.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy DS: " + ex.ToString());
            }
            return list;
        }

        //lấy theo id
        public T GetByID(string id)
        {
            T item = default(T);
            try
            {
                String query = this.GetQueryGetByID(id);
                DataTable dt = DataProvider.ExecuteQuery(query);
                if (dt.Rows.Count > 0)
                {
                    item = GetObjectFromDataRow(dt.Rows[0]);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi: " + ex.ToString());
            }
            return item;
        }

        //thêm
        public Boolean Them2(ref T item)
        {
            Boolean kq = false;
            try
            {
                String strQuery = this.GetInsertQuery();
                IList<OleDbParameter> arrParameter = this.GetListParameter(item);
                DataTable dt = DataProvider.ExecuteNonQuery(strQuery, arrParameter);
                kq = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return kq;
        }

        //cập nhật
        public Boolean CapNhat(ref T item)
        {
            Boolean kq = false;
            try
            {
                String strQuery = this.GetUpdateQuery();
                IList<OleDbParameter> arrParameter = this.GetListParameter(item);
                DataTable dt = DataProvider.ExecuteNonQuery(strQuery, arrParameter);
                kq = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return kq;
        }

        //xóa
        public Boolean Xoa(T item)
        {
            Boolean kq = false;
            try
            {
                String strQuery = this.GetDeleteQuery();
                IList<OleDbParameter> arrParameter = this.GetListParameter(item);
                DataTable dt = DataProvider.ExecuteNonQuery(strQuery, arrParameter);
                kq = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return kq;
        }
    }
}
